package dummy.virussim

import mars.ns.visualisation.VisualisationListener
import mars.ns.core._
import mars.ns.visualisation.NetworkVisualizer
import mars.ns.visualisation.PrefuseNetworkVisualizer

import java.awt.Color
class VirusVisualisation[T <: Node with InfectedState](net:Network[T]) extends VisualisationListener[T](net) {
	val infectedColor = Color.RED
	val immuneColor = Color.WHITE
	val healthyColor = Color.GREEN

	override def init {
		this.nvis = new PrefuseNetworkVisualizer(net)
		nvis.visualise
		net.getNodes.foreach(arg => 
		    if(arg.isImmune) nvis.markNode(arg, immuneColor)
		    else if(arg.isInfected) nvis.markNode(arg, infectedColor)
		    else nvis.markNode(arg, healthyColor))
		this.first = false
	}
	override def edgeAdded(from : T, to:T, lType :LinkType) {
		// synchronized, else init can be executed multiple times
		synchronized{
			if(this.first) init
			this.updated = true
			nvis.addEdge(from, to, lType)
		}
	}
	override def nodeChanged(node :T){
		synchronized{
			if(this.first) init
			this.updated = true
			nvis.setNodeName(node, node.toString)
			if(node.isImmune) nvis.markNode(node, immuneColor)
			else if(node.isInfected)	nvis.markNode(node, infectedColor)			
			else nvis.markNode(node, healthyColor)
			
		}
	}
}